<%--
  Created by IntelliJ IDEA.
  User: tum0r
  Date: 2019-03-13
  Time: 14:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="util.Decode" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="net.sf.json.JSONArray" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="server.*" %>
<%@ page import="config.ApplicationConfig" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
    request.setCharacterEncoding(pageConfig.get("encode"));
    response.setCharacterEncoding(pageConfig.get("encode"));
    Object user = session.getAttribute("user");
    String username = request.getParameter("user");
    UserServer userServer = new UserServer();
    if (username == null || username.equals("")) {
        session.setAttribute("infoTitle", "搜索队伍");
        session.setAttribute("infoContent", "请输入搜索的队伍名");
        session.setAttribute("return", "index.jsp");
        session.setAttribute("panelColor", "red");
        response.sendRedirect("information.jsp");
        return;
    } else {
        userServer.open();
        if (userServer.fuzzySearchUserByName(username).size() == 0) {
            session.setAttribute("infoTitle", "搜索队伍");
            session.setAttribute("infoContent", "搜索的队伍不存在");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            response.sendRedirect("information.jsp");
            return;
        }
        userServer.close();
    }
%>
<html>
<head>
    <title>信 息</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/page.css">
    <link rel="stylesheet" href="css/search.css">
    <script src="js/echarts.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse head" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#navbar">
                <span class="sr-only">切换导航</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <%
                SettingServer settingServer = new SettingServer();
                settingServer.open();
                JSONObject logo = settingServer.readSettingObject("logo");
                if (logo.getString("logo").equals("")) {
            %>
            <img class="navbar-brand" src="image/logo.png">
            <%
            } else {
                FileServer fileServer = new FileServer();
                fileServer.open();
            %>
            <img class="navbar-brand" src="<%=fileServer.getImage(logo.getString("logo"))%>">
            <%
                    fileServer.close();
                }
            %>
            <a class="navbar-brand" href="#"><%=logo.getString("name")%>
            </a>
        </div>
        <div class="collapse navbar-collapse" id="navbar">
            <ul class="nav navbar-nav">
                <li>
                    <a href="index.jsp">首 页</a>
                </li>
                <li>
                    <a href="game.jsp">比 赛</a>
                </li>
                <li>
                    <a href="rank.jsp">排 名</a>
                </li>
                <li>
                    <a href="about.jsp">通 知</a>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right text-center">
                <%
                    if (user == null || user.toString().equals("")) {
                %>
                <li>
                    <a href="#" class="glyphicon glyphicon-log-in" data-toggle="modal" data-target="#login">&nbsp;登录</a>
                    <div class="modal fade" id="login" role="dialog" aria-hidden="true" aria-labelledby="loginLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('login_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="loginLabel">登 录</h4>
                                </div>
                                <div class="modal-body">
                                    <form id="login_form" action="login" method="post">
                                        <input type="text" name="return" hidden="hidden" value="rank.jsp">
                                        用户名:&nbsp;&nbsp;<input type="text" name="username">
                                        <br>
                                        <br>
                                        密&nbsp;&nbsp;&nbsp;码:&nbsp;&nbsp;<input type="password" name="password">
                                        <br>
                                        <br>
                                        验证码:&nbsp;&nbsp;<input type="text" name="ver_code" class="ver_text">
                                        <img src="ver_code" class="ver_img" id="login_ver_code_img"
                                             onclick="reload_code(document.getElementById('login_ver_code_img'))">
                                        <br>
                                        <br>
                                        <font color="red">验证码不区分大小写，点击图片刷新</font>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-danger"
                                            onclick="form_clear(document.getElementById('login_form'))">重 填
                                    </button>
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('login_form'))">登 录
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <a href="#" class="glyphicon glyphicon-user" data-toggle="modal"
                       data-target="#register">&nbsp;注册</a>
                    <div class="modal fade" id="register" role="dialog" aria-hidden="true"
                         aria-labelledby="registerLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('register_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="registerLabel">注 册</h4>
                                </div>
                                <div class="modal-body">
                                    <form id="register_form" action="register" method="post">
                                        <input type="text" name="return" hidden="hidden" value="subject.jsp">
                                        用&nbsp;&nbsp;户&nbsp;&nbsp;名:&nbsp;&nbsp;<input type="text" name="username">
                                        <br>
                                        <br>
                                        密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:&nbsp;&nbsp;<input type="password"
                                                                                                        name="password">
                                        <br>
                                        <br>
                                        再次输入:&nbsp;&nbsp;<input type="password" name="again">
                                        <br>
                                        <br>
                                        验&nbsp;&nbsp;证&nbsp;&nbsp;码:&nbsp;&nbsp;<input type="text" name="ver_code"
                                                                                       class="ver_text">
                                        <img src="ver_code" class="ver_img" id="register_ver_code_img"
                                             onclick="reload_code(document.getElementById('register_ver_code_img'))">
                                        <br>
                                        <br>
                                        <font color="red">更多个人信息请在注册成功后及时进行更新</font>
                                        <br>
                                        <br>
                                        <font color="red">验证码不区分大小写，点击图片刷新</font>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-danger"
                                            onclick="input_clear(document.getElementById('register_form'))">重 填
                                    </button>
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('register_form'))">注 册
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <%
                } else {
                %>
                <li>
                    <a href="mine" class="glyphicon glyphicon-user">&nbsp;<%=user%>
                    </a>
                </li>
                <li>
                    <a href="#" class="glyphicon glyphicon-log-out" data-toggle="modal"
                       data-target="#logout">&nbsp;退出</a>
                    <div class="modal fade" id="logout" role="dialog" aria-hidden="true"
                         aria-labelledby="logoutLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('logout_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="logoutLabel">退 出 登 录</h4>
                                </div>
                                <div class="modal-body">
                                    确定要退出吗？
                                    <form id="logout_form" action="logout" method="post">
                                        <input type="text" name="username" hidden="hidden" value="<%=username%>">
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('logout_form'))">&nbsp;退出
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <%
                    }
                %>
            </ul>
        </div>
    </div>
</nav>

<div class="container text-center">
    <form class="navbar-form navbar-center" role="search" method="post" action="search.jsp" accept-charset="UTF-8">
        <div class="form-group">
            <input type="text" class="form-control text-center" placeholder="搜索队伍" name="user" value="<%=username%>">
        </div>
        <button type="submit" class="btn btn-primary"> 搜 索</button>
    </form>
</div>

<div class="container main text-center">
    <%
        userServer.open();
        JSONArray userArray = userServer.fuzzySearchUserByName(username);
        JSONObject result;
        String searchUsername;
        JSONArray resultArray;
        LinkedList<LinkedHashMap<String, Object>> temp;
        Iterator<LinkedHashMap<String, Object>> userRecordResultIterator;
        LinkedHashMap<String, Object> items;
        long sum;
        long count;
        for (int index = 0; index < userArray.size(); index++) {
            result = userArray.getJSONObject(index);
            searchUsername = result.getString("username");
    %>
    <br>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h2 class="panel-title collapse_mouse" data-toggle="collapse"
                data-target="#<%=searchUsername%>"><%=searchUsername%>
            </h2>
        </div>
        <div id="<%=searchUsername%>" class="panel-collapse collapse">
            <div class="panel-body">
                <%
                    if (JSONObject.fromObject(result.getString("icon")).size() != 0) {
                        JSONObject icon = JSONObject.fromObject(result.getString("icon"));
                        FileServer fileServer = new FileServer();
                        fileServer.open();
                        String uuid = icon.getString("uuid");
                        String name = icon.getString("name");
                %>
                <img src="<%=fileServer.getImage(uuid)%>" class="img-circle icon_mouse" alt="<%=name%>"
                     width=170px height=170px style="border: darkorange 10px solid">
                <%
                    fileServer.close();
                } else {
                %>
                <img src="image/default_icon.png" class="img-circle icon_mouse" alt="default" width=170
                     height=170 style="border: darkorange 10px solid">
                <%
                    }
                %>
                <br>
                <br>
                <hr class="split_line">
                <br>
                <br>
                <%
                    resultArray = userServer.getUserRecordResultAnalysis(searchUsername);
                    result = resultArray.getJSONObject(0);
                %>
                <h2>提交记录</h2>
                <br>
                <br>
                <table class="table table-striped text-center word_break">
                    <tr>
                        <td><b>提交序号</b></td>
                        <td><b>题目</b></td>
                        <td><b>使用语言</b></td>
                        <td><b>结果</b></td>
                        <td><b>时间</b></td>
                        <td><b>内存</b></td>
                        <td><b>提交时间</b></td>
                    </tr>
                    <%
                        temp = userServer.getUserRecordResult(searchUsername);
                        userRecordResultIterator = temp.iterator();
                        SubjectServer subjectServer = new SubjectServer();
                        subjectServer.open();
                        sum = Long.parseLong(result.getString("sum"));
                        count = temp.size();
                        while (userRecordResultIterator.hasNext()) {
                            items = userRecordResultIterator.next();
                    %>
                    <tr>
                        <td><%=count%>
                        </td>
                        <td>
                            <%=subjectServer.getSubjectInformationByID(items.get("subject_id").toString()).getString("title")%>
                        </td>
                        <td><%=items.get("language").toString().replace("CPP","C++")%>
                        </td>
                        <td>
                            <%
                                String submitResult = items.get("result").toString();
                                if (submitResult.equals("Accepted")) {
                            %>
                            <span class="btn btn-success"><%=submitResult%></span>
                            <%
                            } else if (submitResult.equals("Compilation Error") || submitResult.equals("Time Limit Exceeded") || submitResult.equals("Memory Limit Exceeded") || submitResult.equals("Presentation Error")) {
                            %>
                            <span class="btn btn-warning"><%=submitResult%></span>
                            <%
                            } else if (submitResult.equals("Compile...")||submitResult.equals("Running...")) {
                            %>
                            <span class="btn btn-info"><%=submitResult%></span>
                            <%
                            } else if (submitResult.equals("RunTime Error")) {
                            %>
                            <span class="btn btn-primary"><%=submitResult%></span>
                            <%
                            } else {
                            %>
                            <span class="btn btn-danger"><%=submitResult%></span>
                            <%
                                }
                            %>
                        </td>
                        <td><%=items.get("time")%> ms
                        </td>
                        <td><%=items.get("memory")%> kb
                        </td>
                        <td><%=items.get("submit_time")%>
                        </td>
                    </tr>
                    <%
                            count--;
                        }
                        subjectServer.close();
                    %>
                </table>
                <br>
                <br>
                <div id="myResult<%=searchUsername%>" class="my_result"></div>
                <script>
                    var myResult = echarts.init(document.getElementById('myResult<%=searchUsername%>'));
                    myResult.setOption({
                        title: {
                            left: 'center',
                            right: 'center'
                        },
                        series: [
                            {
                                name: '提交记录',
                                type: 'pie',
                                radius: ['25%', '50%'],
                                data: [
                                    <%
                                    Iterator iterator = result.keys();
                                    String key;
                                    if(result.size()==0){
                                        %>
                                    {
                                        value: 0,
                                        name: '\n\n无提交记录'
                                    }
                                    <%
                                        }
                                        while(iterator.hasNext()){
                                            key = iterator.next().toString();
                                            if(!key.equals("sum")){
                                            count = Long.parseLong(result.getString(key));
                                        %>
                                    {
                                        value:<%=count%>,
                                        name: '\n\n\n\n<%=key%>\n\n<%=count%> 次 / <%=sum%> 次\n\n占比 <%=((int)(((double)count/sum)*10000))/100.0%>%\n\n\n\n'
                                    },
                                    <%
                                    }
                                        }
                                    %>
                                ]
                            }
                        ]
                    });
                </script>
                <br>
                <br>
            </div>
        </div>
    </div>
    <%
        }
    %>
</div>
<%
    userServer.close();
    Decode decode = new Decode();
    JSONArray foot = settingServer.readSettingArray("foot");
    JSONObject foot0 = foot.getJSONObject(0);
    JSONObject foot1 = foot.getJSONObject(1);
    JSONObject foot2 = foot.getJSONObject(2);
%>
<div class="foot">
    <row>
        <div class="col-xs-4 col-sm-4">
            <br>
            <div class="foot_item_title">
                <p><%=decode.utf8Decode(foot0.getString("title"))%>
                </p>
            </div>
            <hr class="foot_split">
            <div class="foot_item">
                <p><%=decode.utf8Decode(foot0.getString("line0"))%>
                </p>
                <p><%=decode.utf8Decode(foot0.getString("line1"))%>
                </p>
                <p><%=decode.utf8Decode(foot0.getString("line2"))%>
                </p>
            </div>
        </div>
        <div class="col-xs-4 col-sm-4">
            <br>
            <div class="foot_item_title">
                <p><%=decode.utf8Decode(foot1.getString("title"))%>
                </p>
            </div>
            <hr class="foot_split">
            <div class="foot_item">
                <p><%=decode.utf8Decode(foot1.getString("line0"))%>
                </p>
                <p><%=decode.utf8Decode(foot1.getString("line1"))%>
                </p>
                <p><%=decode.utf8Decode(foot1.getString("line2"))%>
                </p>
            </div>
        </div>
        <div class="col-xs-4 col-sm-4">
            <br>
            <div class="foot_item_title">
                <p><%=decode.utf8Decode(foot2.getString("title"))%>
                </p>
            </div>
            <hr class="foot_split">
            <div class="foot_item">
                <p><%=decode.utf8Decode(foot2.getString("line0"))%>
                </p>
                <p><%=decode.utf8Decode(foot2.getString("line1"))%>
                </p>
                <p><%=decode.utf8Decode(foot2.getString("line2"))%>
                </p>
            </div>
        </div>
    </row>
</div>
<%
    settingServer.close();
%>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/page.js"></script>
</html>
